//封装倒计时逻辑函数
import { ref, computed, onUnmounted } from 'vue'
import dayjs from 'dayjs'
export const useCountDown = ()=>{
  const time = ref(0)
  //格式化时间为xx分xx秒
  const formatTime = computed(() => dayjs.unix(time.value).format('mm分ss秒'))

  let t1 = ref(null)
  //开启倒计时的函数
  const start = (t) => {
    time.value = t
    t1 = setInterval(() => {
      time.value--
      if (time.value <= 0) {
        clearInterval(t1)
      }
    }, 1000)
  }
  onUnmounted(() => {
    t1 && clearInterval(t1)
  })
  return { formatTime, start }
}